package com.jn.algorithm.day010.question30;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author 江南大学1033190417
 * @date 2022/4/25 21:24
 */
public class Solution {
    //暴力解法
    /*
       依次从s中截取固定长度的字符串，然后遍历words数组，判断截取的字符串中是否都包含每一个单词
     */
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> res = new ArrayList<>();
        int wordsLen = words.length;
        int wordLen = words[0].length();
        int len = wordLen * wordsLen;
        for (int i = 0; i <= s.length() - len; i++) {
            String sub = s.substring(i, i + len);
            List<String> word = new ArrayList<>(Arrays.asList(words));
            for (int j = 0; j <= len - wordLen; j=j+wordLen) {
                String temp = sub.substring(j, j + wordLen);
                boolean remove = word.remove(temp);
                if (!remove){
                    break;
                }
            }
            if (word.size()==0){
                res.add(i);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = "barfoofoobarthefoobarman";//24
        String[] words = new String[]{"bar", "foo", "the"};
        System.out.println(solution.findSubstring(s, words));
    }
}
